package com.aaa.mbs.service.impl;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import com.aaa.mbs.dao.AccountStatementDao;
import com.aaa.mbs.dao.MemberDao;
import com.aaa.mbs.entity.AccountStatement;
import com.aaa.mbs.entity.Member;
import com.aaa.mbs.service.AccountStatementService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (AccountStatement)表服务实现类
 *
 * @author makejava
 * @since 2020-12-05 14:29:31
 */
@Service("accountStatementService")
@SuppressWarnings("all")
public class AccountStatementServiceImpl implements AccountStatementService {

    @Resource
    private AccountStatementDao accountStatementDao;

    @Autowired
    private MemberDao memberDao;

    /**流水表中的金额 Amount充值到客户表的Account——Balance字段中
     *
     * @param accountStatement
     * @return
     */
    @Transactional
    @Override
    public Result addToBalance(AccountStatement accountStatement) {
        //关联金额,原始的余额+流水表中的Amount金额值 得到账户余额
        if (accountStatement.getAmount()!=0){
            //Member member = new Member();
//            System.out.println(accountStatement);
            accountStatementDao.insertAmount(accountStatement);
            //新增带的有主键
            //得到流水表充值金额，充值的金额等于成长值
            double amount = accountStatement.getAmount();
            //通过id从流水表中的一条数据
            Member member = memberDao.queryById (accountStatement.getClientId ());
            //得到客户表的原始成长值
            double growth = member.getGrowth ( );
            //客户表新的成长值
            member.setGrowth (amount+growth);
            //客户表原始的余额
            double accountBalance = member.getAccountBalance ();
            //将余额添加到客户表的账户余额字段中,修改余额
            member.setAccountBalance((int) (amount+accountBalance));
            //根据积分规则设置积分
            int addIntegration = 0;
            if(accountStatement.getAmount()<=2000){
                addIntegration  = (int)(accountStatement.getAmount()/10);
            }else if(accountStatement.getAmount()<5000){
                addIntegration = 500;
            }else if(accountStatement.getAmount()<10000){
                addIntegration = 1000;
            }else {
                addIntegration = 3000;
            }
            int oldIntegration= member.getIntegration()==null?0:member.getIntegration();
            //设置积分
            member.setIntegration(oldIntegration+addIntegration);
            //关联客户表Id和流水表Id
            member.setId (accountStatement.getClientId ());
            //修改客户表中的余额
            int i = memberDao.update (member);
            return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                    ReturnStatus.SUCCESS.getReturnMsg(),member);
        }
        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AccountStatement queryById(Integer id) {
        return this.accountStatementDao.selectById(id);
    }

    /**
     * 通过客户id查询相关明细信息
     * @param clientId
     * @return
     */
    @Override
    public Result selectByClientId(Integer clientId) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),accountStatementDao.selectById(clientId));
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param accountStatement 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, AccountStatement accountStatement) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.accountStatementDao.queryAll(accountStatement));
    }

    /**
     * 查询所有
     * @param accountStatement
     * @return
     */
    @Override
    public List<AccountStatement> selectAll(AccountStatement accountStatement) {
        return accountStatementDao.queryAll(accountStatement);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<AccountStatement> queryAllByLimit(int offset, int limit) {
        return this.accountStatementDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param accountStatement 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(AccountStatement accountStatement) {
        return accountStatementDao.insert(accountStatement);
    }

    /**
     * 修改数据
     *
     * @param accountStatement 实例对象
     * @return 实例对象
     */
    @Override
    public AccountStatement update(AccountStatement accountStatement) {
        this.accountStatementDao.update(accountStatement);
        return this.queryById(accountStatement.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.accountStatementDao.deleteById(id) > 0;
    }

    /**
     * 网站收入统计
     * @param yearMonth
     * @return
     */
    @Override
    public Result incomeRecordTotal(String yearMonth) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(),accountStatementDao.incomeRecordTotal(yearMonth));
    }
}